package com.seed.dev.interceptor;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

public class CheckLoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(handler instanceof HandlerMethod handlerMethod) {
            Method method = handlerMethod.getMethod();
            Class<?> controllerClass = method.getDeclaringClass();
            SaIgnore saIgnore = method.getAnnotation(SaIgnore.class);
            if(saIgnore == null) saIgnore = controllerClass.getAnnotation(SaIgnore.class);
            if(saIgnore == null) StpUtil.checkLogin();
        }

        return true;
    }
}
